package com.ygu.mylist.adapter;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import com.ygu.mylist.R;
import com.ygu.mylist.activities.Current_Fragment;
import com.ygu.mylist.db.DataBaseHelper;
import com.ygu.mylist.db.DBQueryResult;
import com.ygu.mylist.db.OprateDataBases;
import com.ygu.mylist.utils.DateTimePickDialogUtil;

import android.app.Activity;
import android.app.Dialog;
import android.app.Fragment;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.TimePicker;

public class Main_List_Adapter extends BaseAdapter implements OnClickListener,OnCheckedChangeListener {
	private Current_Fragment lf;
	private List<DBQueryResult> list;
	private DataBaseHelper dbh;
	private OprateDataBases opd;


	public Main_List_Adapter(Current_Fragment lf,List<DBQueryResult> list){
		this.lf=lf;
		this.list=list;
	}
	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		//this.position=position;
		ViewCache viewCache = null;
		if (convertView == null) {
			viewCache=new ViewCache();

			// LayoutInflater布局填充器
			convertView=LayoutInflater.from(lf.getActivity()).inflate(R.layout.lv_item, null);
			

			// 根据布局并通过控件各自的ID找控件。
			viewCache.content=(TextView) convertView.findViewById(R.id.content_item);
			viewCache.remind_time=(TextView) convertView.findViewById(R.id.remindtime);
			viewCache.clock=(ImageButton) convertView.findViewById(R.id.clock);
			viewCache.finish=(CheckBox) convertView.findViewById(R.id.finish);
			convertView.setTag(viewCache);
			
		} else {
			
			
			viewCache = (ViewCache) convertView.getTag();
			
		}
		viewCache.clock.setTag(list.get(position).getId());
		viewCache.finish.setTag(list.get(position).getId());
		// 给TextView设置内容
		viewCache.content.setText(list.get(position).getContent());
		if(list.get(position).getRemind_time()!=null){
		viewCache.remind_time.setText("提醒于:"+list.get(position).getRemind_time());}
		viewCache.clock.setOnClickListener(this);
		viewCache.finish.setOnCheckedChangeListener(this);
		
		return convertView;
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub	
		dbh=new DataBaseHelper(lf.getActivity());
		opd=new OprateDataBases(dbh.getWritableDatabase());
		
			final int tag;
			tag=(Integer) v.getTag();
			Calendar c = Calendar.getInstance();
			String minitDateTime;
			minitDateTime = list.get(tag-1).getRemind_time();
			DateTimePickDialogUtil datetimepickerdialog=new DateTimePickDialogUtil(lf.getActivity(), minitDateTime);
			datetimepickerdialog.dateTimePicKDialog(opd,lf,tag);
			
	}
	class ViewCache{
		TextView content,remind_time;
		ImageButton clock;
		CheckBox finish;
	}
	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		// TODO Auto-generated method stub
		if(isChecked){
			int tag=(Integer) buttonView.getTag();
			dbh=new DataBaseHelper(lf.getActivity());
			opd=new OprateDataBases(dbh.getWritableDatabase());
			opd.update_status(2, tag);
			lf.refreshAdapter();
			buttonView.setChecked(false);
		}
		
	}

}
